#define _CRT_SECURE_NO_WARNINGS 1
#include <iostream>
using namespace std;
#include <string>
#include <map>
#include <set>
#include <vector>

class Solution1 {
public:
    int lengthOfLongestSubstring(string s) {
        map<char, int> mp;
        int n = 0;
        for (int i = 0;i < s.size();i++)
        {
            if (++mp[s[i]] == 2 || i == s.size() - 1)
            {
                int tmp = mp.size();
                if (n < tmp)
                    n = tmp;

                auto it = mp.find(s[i]);
                if (it != mp.end())
                    it++;
                mp.erase(mp.begin(), it);



                mp[s[i]] = 1;

            }
        }

        
        return n;
    }
};
class Solution {
public:
    int longestOnes(vector<int>& nums, int k) {
        int left = 0;
        int n = 0;
        int max_k = 0;
        for (int right = 0; right < nums.size(); right++)
        {
            if (nums[right] == 0 && n <= k)
            {
                n++;
            }
            else
            {
                max_k = max(max_k, right - left);

                if (nums[left] == 0)
                {
                    n--;

                    left++;
                }
                else
                {
                    while (n == k && nums[left] && left <= right)
                    {
                        left++;
                      
                    }
                }
            }
        }

        return max_k;
    }
};
int main()
{
    Solution S;
    vector<int> v = { 1,1,1,0,0,0,1,1,1,1,0 };
    char arr[] = " ";
    cout<<S.longestOnes(v,3);

	return 0;
}